﻿using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageDevicetable.Command
{
    public class AddManageDeviceCommand : IRequest<Json<string>>
    {
        /// <summary>
        /// 设备编码
        /// </summary>
        public string device { get; set; }
        /// <summary>
        /// 客户账号
        /// </summary>
        public string useraccount { get; set; }
    }

    public class AddManageDeviceCommandHandler : IRequestHandler<AddManageDeviceCommand, Json<string>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IUsertableRepository _iusertableRepository;
        private readonly IDevicetableRepository _ideviceRepository;
        public AddManageDeviceCommandHandler(ICurrentUserService icurrentUserServices, IUsertableRepository iusertableRepository, IDevicetableRepository ideviceRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _iusertableRepository = iusertableRepository;
            _ideviceRepository = ideviceRepository;
        }
        public async Task<Json<string>> Handle(AddManageDeviceCommand request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(request.device))
            {
                return Json<string>.Failed("设备编码不能为空", "");
            }
            else if ((await _ideviceRepository.GetDeviceCount(0, request.device, cancellationToken)) > 0)
            {
                return Json<string>.Failed("当前设备编码已经存在，请修改", "");
            }
            else
            {
                int userid = _icurrentUserServices.GetUserId();

                int bindUserid = 0;
                if (!string.IsNullOrWhiteSpace(request.useraccount))
                {
                    bindUserid = await _iusertableRepository.GetUserTableIdByAccount(userid, request.useraccount, cancellationToken);
                    if (bindUserid <= 0)
                    {
                        return Json<string>.Failed("请输入正确的客户名", "");
                    }

                }
                devicetable model = new devicetable()
                {
                    deviceid = request.device,
                    userid = userid,
                    binduserid = bindUserid,
                    capacitythreshold = 0,
                    createtime = DateTime.Now,
                    currentcapacity = 0,
                    cusdeviceid = "",
                    maxcapacity = 0,
                    remark = "",
                    skuid = 0,
                    state = 0,
                    updatetime = DateTime.Now,
                    userpid = userid
                };
                await _ideviceRepository.AddDeviceTable(model, cancellationToken);
                return Json<string>.Success("添加成功", "");
            }
        }
    }
}
